/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Yerong
 */
public class LoanGUI extends javax.swing.JFrame {

    /**
     * 
     * Creates new form LoanGUI
     */
    public LoanGUI() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        counterJLabel = new javax.swing.JLabel();
        counterJTextField = new javax.swing.JTextField();
        principalJLabel = new javax.swing.JLabel();
        principalTextField = new javax.swing.JTextField();
        rateJLabel = new javax.swing.JLabel();
        rateTextField = new javax.swing.JTextField();
        yearsJLabel = new javax.swing.JLabel();
        yearsTextField = new javax.swing.JTextField();
        CalculateButton = new javax.swing.JButton();
        paymentTextfield = new javax.swing.JTextField();
        interestJlabel = new javax.swing.JLabel();
        interestJTextField = new javax.swing.JTextField();
        clearJBotton = new javax.swing.JButton();
        clearJTextField = new javax.swing.JButton();

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jScrollPane1.setViewportView(jTextArea1);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("principalJLabel");
        getContentPane().setLayout(new java.awt.GridLayout(7, 2));

        counterJLabel.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        counterJLabel.setText("Calculation Counter");
        getContentPane().add(counterJLabel);

        counterJTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                counterJTextFieldActionPerformed(evt);
            }
        });
        getContentPane().add(counterJTextField);

        principalJLabel.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        principalJLabel.setText("Enter the loan amount");
        getContentPane().add(principalJLabel);
        getContentPane().add(principalTextField);

        rateJLabel.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        rateJLabel.setText("Enter interest rate as a percent");
        rateJLabel.setName("rateJLabel"); // NOI18N
        getContentPane().add(rateJLabel);

        rateTextField.setName("rateJTextField"); // NOI18N
        getContentPane().add(rateTextField);

        yearsJLabel.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        yearsJLabel.setText("Enter years of loan");
        yearsJLabel.setName("yearsJLabel"); // NOI18N
        getContentPane().add(yearsJLabel);

        yearsTextField.setName("yearsJTextField"); // NOI18N
        getContentPane().add(yearsTextField);

        CalculateButton.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        CalculateButton.setText("Calculate Payment");
        CalculateButton.setName("calculateJButton"); // NOI18N
        CalculateButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CalculateButtonActionPerformed(evt);
            }
        });
        getContentPane().add(CalculateButton);

        paymentTextfield.setEditable(false);
        paymentTextfield.setName("paymentJTextField"); // NOI18N
        paymentTextfield.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                paymentTextfieldActionPerformed(evt);
            }
        });
        getContentPane().add(paymentTextfield);

        interestJlabel.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        interestJlabel.setText("Interest paid");
        getContentPane().add(interestJlabel);

        interestJTextField.setEditable(false);
        interestJTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                interestJTextFieldActionPerformed(evt);
            }
        });
        getContentPane().add(interestJTextField);

        clearJBotton.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        clearJBotton.setText("Clear");
        clearJBotton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                clearJBottonActionPerformed(evt);
            }
        });
        getContentPane().add(clearJBotton);

        clearJTextField.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        clearJTextField.setText("Quit");
        clearJTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                clearJTextFieldActionPerformed(evt);
            }
        });
        getContentPane().add(clearJTextField);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void CalculateButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CalculateButtonActionPerformed
//calculate payment on a loantry
{//get inputs
double amount = Double.parseDouble(principalTextField.getText());
double rate = Double.parseDouble(rateTextField.getText());
double years = Double.parseDouble(yearsTextField.getText());
boolean invalidInputs = (amount < 0 || amount > 100000000 ||
rate < 0 || rate > 100 || years < 0 || years > 100);
if (invalidInputs)
throw new NumberFormatException();
else
{
//calculate payment using formula
counter++;
double payment = (amount * rate/1200)/(1 -
Math.pow((1 + rate/1200),years * (-12)));
double interest = years * payment * 12 - amount;
DecimalFormat dollars = new DecimalFormat("$#,##0.00");
String result = dollars.format(payment);
//display results
counterJTextField.setText(String.valueOf(counter));
paymentJTextField.setText(result);
interestJTextField.setText(dollars.format(interest));
}
}
catch(NumberFormatException nume)
{
JOptionPane.showMessageDialog(null,
"Please enter a positive number for all required fields",
"Input Error", JOptionPane.WARNING_MESSAGE);
principalJTextField.requestFocus();
principalJTextField.selectAll();
}
    }//GEN-LAST:event_CalculateButtonActionPerformed

    private void paymentTextfieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_paymentTextfieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_paymentTextfieldActionPerformed

    private void interestJTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_interestJTextFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_interestJTextFieldActionPerformed

    private void clearJBottonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_clearJBottonActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_clearJBottonActionPerformed

    private void clearJTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_clearJTextFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_clearJTextFieldActionPerformed

    private void counterJTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_counterJTextFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_counterJTextFieldActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(LoanGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(LoanGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(LoanGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(LoanGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new LoanGUI().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton CalculateButton;
    private javax.swing.JButton clearJBotton;
    private javax.swing.JButton clearJTextField;
    private javax.swing.JLabel counterJLabel;
    private javax.swing.JTextField counterJTextField;
    private javax.swing.JTextField interestJTextField;
    private javax.swing.JLabel interestJlabel;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JTextField paymentTextfield;
    private javax.swing.JLabel principalJLabel;
    private javax.swing.JTextField principalTextField;
    private javax.swing.JLabel rateJLabel;
    private javax.swing.JTextField rateTextField;
    private javax.swing.JLabel yearsJLabel;
    private javax.swing.JTextField yearsTextField;
    // End of variables declaration//GEN-END:variables
}
